package com.ivan.toolkit.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.text.JTextComponent;

import lombok.Getter;
import lombok.Setter;

public class ConfigDialog extends JDialog {
	
	/**

	 * 

	 */
	private static final long serialVersionUID = -7336211601442964121L;
	
	
	protected JLabel lblMsg = null;
	protected JButton okButton = null;
	protected JButton cancelButton = null;
	
	private boolean finishOk = false;
	
	@Getter @Setter
	private static Image applicationIcon = null;

	/**

	 * Create the dialog.

	 */
	public ConfigDialog() {
		setTitle("\u5BF9\u8BDD\u6846\u6807\u9898");
		if(applicationIcon != null)
			setIconImage(applicationIcon);
		setResizable(false);
		setModal(true);
		setBounds(100, 100, 480, 320);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBorder(new CompoundBorder(new MatteBorder(1, 0, 0, 0, (Color) new Color(128, 128, 128)), new EmptyBorder(5, 5, 5, 5)));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
			{
				lblMsg = new JLabel("");
				buttonPane.add(lblMsg);
			}
			{
				okButton = new JButton("\u786E\u5B9A");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						onOK();
					}
				});
				
				Component horizontalGlue = Box.createHorizontalGlue();
				buttonPane.add(horizontalGlue);
				okButton.setPreferredSize(new Dimension(80, 30));
				okButton.setMinimumSize(new Dimension(60, 30));
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("\u53D6\u6D88");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				
				Component horizontalStrut = Box.createHorizontalStrut(10);
				buttonPane.add(horizontalStrut);
				cancelButton.setPreferredSize(new Dimension(80, 30));
				cancelButton.setMinimumSize(new Dimension(60, 30));
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
	}
	
	public void startAncestor(Component c) {
		start(SwingUtilities.getWindowAncestor(c));
	}
	
	public void start(Component c) {
		this.setLocationRelativeTo(c);
		try {
			this.prepare();
		} catch (Exception e) {
			error("初始化数据失败！");
		}
		this.setVisible(true);
	}
	
	protected void error(String text) {
		throw new RuntimeException(text);
	}
	
	protected void error(Exception e) throws Exception {
		throw e;
	}
	
	protected void errorForTextComonent(JTextComponent comp, String message) throws Exception {
		comp.requestFocus();
		comp.selectAll();
		throw new RuntimeException(message);
	}
	
	public ConfigDialog verifyNotEmpty(JTextComponent comp, String message) throws Exception {
		if(comp.getText().isEmpty()) 
			errorForTextComonent(comp, message);
		return this;
	}
	
	public ConfigDialog verifyEqual(JTextComponent comp1, JTextComponent comp2, String message) throws Exception {
		if(!comp1.getText().equals(comp2.getText())) 
			errorForTextComonent(comp1, message);
		return this;
	}
	
	public ConfigDialog verifyMinLength(JTextComponent comp, int min, String message) throws Exception {
		String text = comp.getText();
		if(text.length() < min)
			errorForTextComonent(comp, message);
		return this;
	}
	
	public ConfigDialog verifyMaxLength(JTextComponent comp, int max, String message) throws Exception {
		String text = comp.getText();
		if(text.length() > max)
			errorForTextComonent(comp, message);
		return this;
	}
	
	public ConfigDialog verifyRange(JTextComponent comp, int min, int max, String message) throws Exception {
		String text = comp.getText();
		if(text.length() < min || text.length() > max)
			errorForTextComonent(comp, message);
		return this;
	}
	
	public ConfigDialog verifyPattern(JTextComponent comp, String pattern, String message) throws Exception {
		String text = comp.getText();
		try {
			Pattern p = Pattern.compile(pattern);
			Matcher m = p.matcher(text);
			if(!m.matches())
				errorForTextComonent(comp, message);
		} catch(Exception e) {
			errorForTextComonent(comp, message);
		}
		return this;
	}
	
	public ConfigDialog verifyMobile(JTextComponent comp, String message) throws Exception {
		String text = comp.getText();
		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(text);
		if(!m.matches())
			errorForTextComonent(comp, message);
		return this;
	}
	
	/*
	public ConfigDialog verifyIPV4(JTextComponent comp, String message) throws Exception {
		String text = comp.getText();
		if(!StringUtil.isIPV4(text))
		//if(!IPAddressUtil.isIPv4LiteralAddress(text))
			errorForTextComonent(comp, message);
		return this;
	}
	*/
	
	public String toText(JTextComponent comp) {
		return comp.getText();
	}
	
	public long toLong(JTextComponent comp, String message) throws Exception {
		try {
			String text = comp.getText();
			return Long.parseLong(text);
		} catch(NumberFormatException e) {
			errorForTextComonent(comp, message);
		}
		return 0;
	}
	
	public int toInteger(JTextComponent comp, String message) throws Exception {
		try {
			String text = comp.getText();
			return Integer.parseInt(text);
		} catch(NumberFormatException e) {
			errorForTextComonent(comp, message);
		}
		return 0;
	}
	
	private void onOK() {
		
		try {
			this.doVerify();
			this.doCommit();
			this.setVisible(false);
			this.setFinishOk(true);
		} catch (Exception e) {
			this.lblMsg.setForeground(Color.RED);
			this.lblMsg.setText(e.getMessage());
			//JOptionPane.showMessageDialog(null, e.getMessage(), "����", JOptionPane.ERROR_MESSAGE);

			return;
		}

	}
	
	protected void prepare() throws Exception {
		
	}
	
	protected void doVerify() throws Exception {
		
	}
	
	protected void doCommit() throws Exception {
		
	}


	public boolean isFinishOk() {
		return finishOk;
	}


	protected void setFinishOk(boolean finishOk) {
		this.finishOk = finishOk;
	}

}
